//table头信息 不变
var umt_columnData = [
    {"Width": "33%", "ColumnName": "用户ID"},
    {"Width": "33%", "ColumnName": "用户名称"},
    {"Width": "33%", "ColumnName": "操作"}
];

//操作按钮 不变
var umt_operateHtml = "<div class='form-inline'><button type='button' class='btn btn-primary innerButtons'" +
    " value='delete'>删除 </button> " +
    " <button type='button' class='btn btn-primary innerButtons' data-toggle='modal' value='authorize'>授权</button>" +
    "</div>";

//弹出table的title 不变
var uat_columnData = [
    {"Width": "10%", "ColumnName": ""},
    {"Width": "40%", "ColumnName": "角色ID"},
    {"Width": "40%", "ColumnName": "角色名称"}
];

//用户管理分页函数
var gotoPage = function (index) {
    var userName = $("#userName").val();
    var url = webPath + "/crm/getUserBypage.do"
    var pageNumber = index;
    var postData = {"username": userName, "pageSize": pageSize, "pageNumber": pageNumber}
    var options = {"passArguments": pageNumber}
    postAjax(url, postData, gotoPageSuccess, options);
};

function gotoPageSuccess(result, passArguments) {
    var columnList = ["userId", "username"];
    var jsonData = $.parseJSON(result);
    cleanTableAndPage("userManageTable");
    //画table
    $("#userManageTable").MakeTable(false, umt_columnData, jsonData.list, tableClass, columnList, umt_operateHtml);
    //画分页
    $("#userManageTable").after(comPagination(passArguments, pageSize, jsonData.totalNumber, "gotoPage", "userManageP"));

    //Table中操作栏 授权（获取授权页面） 删除处理
    $("#userManageTable button").click(function () {
        var that = $(this);
        var buttonType = that.val();
        if (buttonType === "authorize") {
            userAuthorization(that);
        } else if (buttonType === "delete") {
            userDelete(that);
        } else {
        }
    })
};

//查询按钮行为
$('#searchUserName').click(function () {
    gotoPage(1);
});

//用户权限授权提交
$("#userAuthSaveBtn").click(function () {
    var selectRow = $("#userAuthorizationTable tbody").find("input:checked");
    var userId = $("#userIdDisabled").text();
    var roleId = selectRow.closest("tr").find("td div")[1].innerHTML;
    var url = webPath + "/crm/saveAuthUserByRole.do";
    var postData = {"userId": userId, "roleId": roleId};
    var options = {"successArguments": userId, "successMessage": "用户" + userId + "授权成功"};
    postAjax(url, postData, userAuthSaveSuccess, options);
});

var userAuthSaveSuccess = function (argument) {
    $("#userAuthModal").modal('hide');
    showSuccess("授权成功", "授权" + argument + "成功");
    gotoPage(getCurrentPage("userManageTable"));
}

//用户删除
var userDelete = function (that) {
    var userId = getTableFirstColumnId(that);
    var url = webPath + "/crm/deleteUserById.do";
    showDialogueBox({
        'id': 'confirmMsg',
        'type': 'confirm',
        'title': 'delete confirm',
        'message': '你确定要删除吗',
        'buttons': {
            'Yes': {
                'class': 'btn btn-primary',
                'action': function () {
                    var options = {"passArguments": that, "successMessage": "用户" + userId + "删除成功！"}
                    postAjax(url, {"userId": userId}, userDeleteSuccess, options);
                }
            },
            'no': {
                'class': 'btn',
                'action': function () {
                }
            }
        }
    });
}

var userDeleteSuccess = function (result, that) {
    gotoPage(getCurrentPage(that.closest("table").attr("id")));
    showSuccess("删除成功！");
}

//用户授权
var userAuthorization = function (that) {
    var url = webPath + "/crm/authUserByRole.do";
    var userId = getTableFirstColumnId(that);
    var postData = {"userId": userId};
    postAjax(url, postData, userAuthorizationSuccess);
}

var userAuthorizationSuccess = function (result) {
    var columnList = ["roleId", "roleName"];
    cleanTableAndPage("userAuthorizationTable");
    var jsonData = $.parseJSON(result);
    $("#userNameDisabled").text(jsonData.userName);
    $("#userIdDisabled").text(jsonData.userId);
    $("#userAuthorizationTable").MakeTable(true, uat_columnData, jsonData.list, tableClass, columnList);
    var tdList = $("#userAuthorizationTable tbody td:nth-child(2)");
    tableCheckBoxChecked(tdList, jsonData.roleId);
    $("#userAuthorizationTable tbody input:checkbox").click(function () {
        var that = this;
        $("#userAuthSaveBtn").attr("disabled", null);
        checkBoxSelectOne(that);
    });
    if ($("#userAuthorizationTable tbody").find("input:checkbox")) {
        $("#userAuthSaveBtn").attr("disabled", true);
    } else {
        $("#userAuthSaveBtn").attr("disabled", null);
    }
    $("#userAuthModal").modal('show');
}

//获取id 方便提交 TODO
var getTableFirstColumnId = function (that) {
    return that.closest("tr").find("td div")[0].innerHTML;
}